<?PHP
/**
 * validate_form validates the form data, checking all legality.
 * it returns an array of the errors with the form (string warnings)
 * this array is null (aka false) if there are no errors
 */
function validate_form(){
	if($_POST['first_name']==null || $_POST['first_name']=='new') $errors[] = 'Please enter a first name';
	if($_POST['last_name']==null || $_POST['first_name']=='vollunteer') $errors[] = 'Please enter a last name';
	if($_POST['city']==null) $errors[] = 'Please enter a city';
	if($_POST['address']==null) $errors[] = 'Please enter an address';
	if(($_POST['zip'] != strval(intval($_POST['zip']))) || ($_POST['zip']==null) || (strlen($_POST['zip'])!=5)) $errors[] = 'Please enter a valid zip code';
	if ($_SESSION['access_level']==0 && ($_POST['type']==null || !in_array('applicant',$_POST['type'])))
		$_POST['type'][] = 'applicant';
	if($_POST['type']==null && $_SESSION['access_level']>=1) $errors[] = 'Select a Position type';
	if ($_POST['type']!=null && $_SESSION['access_level']<=1 && in_array('manager',$_POST['type']))
		$errors[] = "Sorry, you can't promote yourself to manager.";
	if($_POST['availability']==null &&
			($_POST['type']==null || in_array('sub',$_POST['type']) || in_array('volunteer',$_POST['type']) || in_array('applicant',$_POST['type'])
								  || in_array('family_room',$_POST['type']) || in_array('weekend_volunteer',$_POST['type'])))
		$errors[] = 'Must have some availabilty';
	if(!valid_phone($_POST['phone1'])) $errors[] = 'Enter a valid primary phone number (10 digits: ### ### ####)';
	if($_POST['phone2']!=null && !valid_phone($_POST['phone2'])) $errors[] = 'Enter a valid secondary phone number (10 digits: ### ### ####)';
	if(!valid_email($_POST['email']) && $_POST['email']!=null) $errors[] = "Please enter a valid email";
	if(($_POST['DateOfBirth_Month']!=="" || $_POST['DateOfBirth_Day']!=="") && $_POST['DateOfBirth_Year']==="")
		$birthday = $_POST['DateOfBirth_Month'].'-'.$_POST['DateOfBirth_Day'].'-XX';
	else
		$birthday = $_POST['DateOfBirth_Month'].'-'.$_POST['DateOfBirth_Day'].'-'.$_POST['DateOfBirth_Year'];
	if (strlen($birthday)>2 && strlen($birthday)<8) $errors[] = 'Please select a valid birthday';
	$start_date = $_POST['DateOfStart_Month'].'-'.$_POST['DateOfStart_Day'].'-'.$_POST['DateOfStart_Year'];
	if (strlen($start_date)>2 && strlen($start_date)<8) $errors[] = 'Please select a valid start date';
	return $errors;
}


/**
* valid_phone validates a phone on the following parameters:
* 		it assumes the characters '-' ' ' '+' '(' and ')' are valid, but ignores them
*		every other digit must be a number
*		it should be between 7 and 11 digits
* returns boolean if phone is valid
*/
function valid_phone($phone){
		if($phone==null) return false;
		$phone = str_replace(' ','',str_replace('+','',str_replace('(','',str_replace('(','',str_replace('-','',$phone)))));
		$test = str_replace('0','',str_replace('1','',str_replace('2','',str_replace('3','',str_replace('4','',str_replace('5','',str_replace('6','',str_replace('7','',str_replace('8','',str_replace('9','',$phone))))))))));
		if($test != null) return false;
		if ( (strlen($phone)) != 10) return false;
		return true;
}

//Function from <http://www.phpit.net/code/valid-email/>
function valid_email($email) {
		// First, we check that there's one @ symbol, and that the lengths are right
		if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
			// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
			return false;
		}
		// Split it into sections to make life easier
		$email_array = explode("@", $email);
		$local_array = explode(".", $email_array[0]);
		for ($i = 0; $i < sizeof($local_array); $i++) {
			if (!ereg("^(([A-Za-z0-9!#$%&#038;'*+/=?^_`{|}~-][A-Za-z0-9!#$%&#038;'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
				return false;
			}
		}
		if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
			$domain_array = explode(".", $email_array[1]);
			if (sizeof($domain_array) < 2) {
				return false; // Not enough parts to domain
			}
			for ($i = 0; $i < sizeof($domain_array); $i++) {
				if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
					return false;
				}
			}
		}
		return true;
}

function show_errors($e){
		//this function should display all of our errors.
		echo('<div class="warning">');
		echo('<ul>');
		foreach($e as $error){
			echo("<li><strong><font color=\"red\">".$error."</font></strong></li>\n");
		}
		echo("</ul></div></p>");
}
?>